const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    capsule: app.globalData.capsule,
    user: null,
    msg:"欢迎光临~"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    if (options._openid)
      this.getUserInfo(options._openid)
    else
      this.getUserInfo()

  },
  getRecord(_openid) {
    console.log(_openid)
    wx.cloud.callFunction({
      name: 'getRecord',
      data: {
        op: 'myHomeRecord',
        _openid: _openid
      }
    }).then(res => {
      console.log(_openid, 'user-records', res.result)
      this.setData({
        records: res.result
      })
    })
  },
  getUserInfo(_openid) {

    if (this.data.user) {
       _openid = this.data.user._openid
    }
    console.log('更新用户数据', _openid)
    wx.cloud.callFunction({
      name: 'user',
      data: {
        op: 'visit',
        _openid: _openid
      }
    }).then(res => {
      console.log('randomUserHome', res.result)
      let user = res.result
      // let nickname = res.result.nickname
      // user.nickname = nickname.substr(0, 1) + "**" + nickname.substr(nickname.length - 1, 1)
      this.setData({
        user: user,
      })
      this.getRecord(res.result._openid)
    })
  },
  hideMenu(e = null) {
    let name = e ? e.detail : 'kk'
    console.log('xxx', e)
    switch (name) {
      case 'pet':
        this.selectComponent('#house').hideMenu();
        break;
      case 'house':
        this.selectComponent('#pet').hideMenu();
        break;
      default:
        this.selectComponent('#house').hideMenu();
        this.selectComponent('#pet').hideMenu();
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.hideMenu()
  },
  hideMenu(e = null) {
    let name = e ? e.detail : 'kk'
    console.log('xxx', e)
    switch (name) {
      case 'pet':
        this.selectComponent('#house').hideMenu();
        break;
      case 'house':
        this.selectComponent('#pet').hideMenu();
        break;
      default:
        this.selectComponent('#house').hideMenu();
        this.selectComponent('#pet').hideMenu();
    }

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})